﻿using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using Red.Scripts.Defines.Utilities;

namespace Red.Scripts.Options.SccSearchOrder
{
    public partial class Ctrl : UserControl
    {
        #region Public Types

        public class Item
        {
            #region Public Properties

            public Defines.SccSearchOrder Value { get; set; }
            public string Display => Value.GetDescription();

            #endregion Public Properties

            #region Operators

            public static implicit operator Defines.SccSearchOrder( Item item )
            {
                return item.Value;
            }

            public static implicit operator Item( Defines.SccSearchOrder value )
            {
                return new Item() { Value = value };
            }

            #endregion Operators
        }

        #endregion Public Types

        #region Dependency Properties

        public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register( "ItemsSource", typeof( ObservableCollection<Item> ), typeof( Ctrl ), new PropertyMetadata( new ObservableCollection<Item>() ) );
        public static readonly DependencyProperty CustomSearchPathProperty = DependencyProperty.Register( "CustomSearchPath", typeof( string ), typeof( Ctrl ) );

        #endregion Dependency Properties

        #region Public Properties

        public ObservableCollection<Item> ItemsSource
        {
            get { return (ObservableCollection<Item>)GetValue( ItemsSourceProperty ); }
            set { SetValue( ItemsSourceProperty, value ); }
        }

        public string CustomSearchPath
        {
            get { return (string)GetValue( CustomSearchPathProperty ); }
            set { SetValue( CustomSearchPathProperty, value ); }
        }

        #endregion Public Properties

        #region C-Tor

        public Ctrl()
        {
            InitializeComponent();
        }

        #endregion C-Tor

        #region Private Methods

        private void MoveUp( int index )
        {
            if( index > 0 && index < ItemsSource.Count )
            {
                ItemsSource.Move( index, index - 1 );
            }
        }

        private void Up_Click( object sender, RoutedEventArgs e )
        {
            MoveUp( _listbox.SelectedIndex );
        }

        private void Down_Click( object sender, RoutedEventArgs e )
        {
            MoveUp( _listbox.SelectedIndex + 1 );
        }

        private void FilePicker_Click( object sender, RoutedEventArgs e )
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

            dlg.DefaultExt = ".exe";
            dlg.Filter = "Red Scripts Compiler|scc.exe";
            dlg.CheckFileExists = true;
            dlg.Multiselect = false;
            dlg.Title = "Select the location of your Red Scripts Compiler executable";

            if( dlg.ShowDialog() == true )
            {
                CustomSearchPath = dlg.FileName;
            }
        }

        #endregion Private Methods
    }
}
